package com.thrd.basicframework.bean;

import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.widget.PopupWindow;

public class CommonPopupWindow extends PopupWindow {

    private static final int FULL_SCREEN_FLAG =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;


    final PopupController controller;
    private Context context;
    @Override
    public int getWidth() {
        return controller.mPopupView.getMeasuredWidth();
    }

    @Override
    public int getHeight() {
        return controller.mPopupView.getMeasuredHeight();
    }

    public interface ViewInterface {
        void getChildView(View view, int layoutResId);
    }

    private CommonPopupWindow(Context context) {
        this.context=context;
        controller = new PopupController(context, this);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        setAnimention(0.5f,1f);
    }





    public void showAsDropDown(View anchor, int xoff, int yoff,float level) {
        setAnimention(1f,0.5f);
        super.showAsDropDown(anchor, xoff, yoff);

    }

    public void showAtLocation(View anchor, int gravity){
        setAnimention(1f,0.5f);
        setFocusable(false);
        super.showAtLocation(anchor, gravity, 0,0);
        getContentView().setSystemUiVisibility(FULL_SCREEN_FLAG);
        setFocusable(true);
        update();
    }

    public void showAsDropDown(View anchor, int xoff, int yoff) {
        setAnimention(1f,0.5f);
        setFocusable(false);
        super.showAsDropDown(anchor, xoff, yoff);
        getContentView().setSystemUiVisibility(FULL_SCREEN_FLAG);
        setFocusable(true);
        update();


    }

    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity){
       // controller.setBackGroundLevel(0.5f);
        setAnimention(1f,0.5f);
        setFocusable(false);
        super.showAsDropDown(anchor, xoff, yoff,gravity);
        getContentView().setSystemUiVisibility(FULL_SCREEN_FLAG);
        setFocusable(true);
        update();

    }


    private void setAnimention(float start,float level){
        start=start*10;
        level=level*10;
        ValueAnimator valueAnimator= ValueAnimator.ofFloat(start,level);
        valueAnimator.setDuration(800);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              //  LogUtil.e("ValueAnimator","ValueAnimator="+animation.getAnimatedValue());
                float level=((float)animation.getAnimatedValue())/10;
                controller.setBackGroundLevel(level);
            }
        });
        valueAnimator.start();
    }

    public static class Builder {
        private final PopupController.PopupParams params;
        private ViewInterface listener;

        public Builder(Context context) {
            params = new PopupController.PopupParams(context);
        }

        /**
         * @param layoutResId 设置PopupWindow 布局ID
         * @return Builder
         */
        public Builder setView(int layoutResId) {
            params.mView = null;
            params.layoutResId = layoutResId;
            return this;
        }

        /**
         * @param view 设置PopupWindow布局
         * @return Builder
         */
        public Builder setView(View view) {
            params.mView = view;
            params.layoutResId = 0;
            return this;
        }

        /**
         * 设置子View
         *
         * @param listener ViewInterface
         * @return Builder
         */
        public Builder setViewOnclickListener(ViewInterface listener) {
            this.listener = listener;
            return this;
        }

        /**
         * 设置宽度和高度 如果不设置 默认是wrap_content
         *
         * @param width 宽
         * @return Builder
         */
        public Builder setWidthAndHeight(int width, int height) {
            params.mWidth = width;
            params.mHeight = height;
            return this;
        }

        /**
         * 设置背景灰色程度
         *
         * @param level 0.0f-1.0f
         * @return Builder
         */
        public Builder setBackGroundLevel(float level) {
            params.isShowBg = true;
            params.bg_level = level;
            return this;
        }

        /**
         * 是否可点击Outside消失
         *
         * @param touchable 是否可点击
         * @return Builder
         */
        public Builder setOutsideTouchable(boolean touchable) {
            params.isTouchable = touchable;
            return this;
        }

        /**
         * 设置动画
         *
         * @return Builder
         */
        public Builder setAnimationStyle(int animationStyle) {
            params.isShowAnim = true;
            params.animationStyle = animationStyle;
            return this;
        }

        public CommonPopupWindow create() {
            final CommonPopupWindow popupWindow = new CommonPopupWindow(params.mContext);

            params.apply(popupWindow.controller);

            if (listener != null && params.layoutResId != 0) {
                listener.getChildView(popupWindow.controller.mPopupView, params.layoutResId);
            }

//            popupWindow.setFocusable(false);
//            popupWindow.showAtLocation(view,  x,  y);
//            popupWindow.getContentView().setSystemUiVisibility(FULL_SCREEN_FLAG);
//            popupWindow.setFocusable(true);
//            popupWindow.update();
//
//            popupWindow.controller.mPopupView.setSystemUiVisibility(FULL_SCREEN_FLAG);
//            popupWindow.update();
          //  CommonUtil.measureWidthAndHeight(popupWindow.controller.mPopupView);
            return popupWindow;
        }


    }
}
